package com.haisen.service.impl;

import com.alibaba.fastjson.JSON;
import com.haisen.dataobject.entity.FileQueue;
import com.haisen.dataobject.mapper.base.FileQueueMapper;
import com.haisen.service.FileQueueService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.List;

@Service
@Slf4j
public class FileQueueServiceImpl implements FileQueueService {


    @Autowired
    private FileQueueMapper fileQueueMapper;

    @Override
    public List<FileQueue> listOnProceing() {
        Weekend<FileQueue> example = Weekend.of(FileQueue.class);
        WeekendCriteria<FileQueue,Object> criteria = example.weekendCriteria();
        criteria.andEqualTo(FileQueue::getResult,0);
        return fileQueueMapper.selectByExample(example);
    }

    @Override
    public FileQueue loadByPath(String pathFile) {
        Weekend<FileQueue> example = Weekend.of(FileQueue.class);
        WeekendCriteria<FileQueue,Object> criteria = example.weekendCriteria();
        criteria.andEqualTo(FileQueue::getSrcPathFile,pathFile);
        return fileQueueMapper.selectOneByExample(example);
    }

    @Override
    public void updateByTblName(FileQueue fileQueue) {
        Weekend<FileQueue> example = Weekend.of(FileQueue.class);
        WeekendCriteria<FileQueue,Object> criteria = example.weekendCriteria();
        criteria.andEqualTo(FileQueue::getTblName,fileQueue.getTblName());
        //log.info("更新导入文件状态：{}", JSON.toJSONString(fileQueue));
        fileQueueMapper.updateByExampleSelective(fileQueue,example);
    }

    @Override
    public List<FileQueue> findSucess(String tableName) {
        Weekend<FileQueue> example = Weekend.of(FileQueue.class);
        WeekendCriteria<FileQueue,Object> criteria = example.weekendCriteria();
        criteria.andEqualTo(FileQueue::getResult,1);
        criteria.andLike(FileQueue::getTblName,tableName+"%");
        //改成查电白区域的,临时修改2019/9/23
       // criteria.andLike(FileQueue::getSrcPathFile,"%"+tableName+"%");
        return fileQueueMapper.selectByExample(example);
    }

    @Override
    public void create(FileQueue fileQueue) {
       fileQueueMapper.insertSelective(fileQueue);
    }

    @Override
    public void update(FileQueue fileQueue) {
      fileQueueMapper.updateByPrimaryKey(fileQueue);
    }

}
